//
//  HomeController.swift
//  Practice
//
//  Created by miaozhihui on 2017/1/9.
//  Copyright © 2017年 DeKuTree. All rights reserved.
//

import UIKit

/// 原创微博可重用 cell id
private let originalCellId = "originalStatusCell"
/// 被转发微博的可重用 cell id
private let retweetedCellId = "retweetedStatusCell"

class HomeController: BaseController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    /// 微博列表视图模型
    fileprivate lazy var statusListViewModel = StatusListViewModel()
    
    /// 加载数据
    override func loadData() {
        
        statusListViewModel.loadStatus(isPullUp: isPullUp) { (isSuccess,shouldRefresh) in
            print("加载数据结束\(self.statusListViewModel.statusViewModelList.last?.status.text)")
            // 结束刷新控件
//            DispatchQueue.main.async {
                self.refreshControl?.endRefreshing()
//            }
            // 恢复上拉刷新标记
            self.isPullUp = false
            // 刷新表格
            if shouldRefresh {
                self.tableView?.reloadData()
            }
        }
    }
    
    /// 显示好友
    @objc fileprivate func showFriends() {
        //let vc = DemoController()
        //navigationController?.pushViewController(vc, animated: true)
        
        let vc = UINavigationController(rootViewController: OAuthViewController())
        present(vc, animated: true, completion: nil)
        
    }
}

// MARK: -表格数据源方法
extension HomeController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if statusListViewModel.statusViewModelList.count == 0 {
            return super.tableView(tableView, numberOfRowsInSection: section)
        }
        return statusListViewModel.statusViewModelList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 如果没有数据显示默认视图
        if statusListViewModel.statusViewModelList.count == 0 {
            return super.tableView(tableView, cellForRowAt: indexPath)
        }
        // 获取视图模型
        let vm = statusListViewModel.statusViewModelList[indexPath.row]
        let cellId = (vm.status.retweeted_status != nil) ? retweetedCellId : originalCellId
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! StatusCell
        cell.viewModel = vm
        cell.delegate = self
        return cell
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if statusListViewModel.statusViewModelList.count == 0 {
            return super.tableView(tableView, heightForRowAt: indexPath)
        } else {
            let vm = statusListViewModel.statusViewModelList[indexPath.row]
            return vm.rowHeight
        }
    }
}

extension HomeController: StatusCellDelegate {
    func statusCellDidSelectedURLString(cell: StatusCell, urlString: String) {
        
        let vc = WebViewController()
        vc.urlString = urlString
        navigationController?.pushViewController(vc, animated: true)
    }
}

// MARK: - 设置界面
extension HomeController {
    
    /// 重写父类方法
    override func setupTableView() {
        super.setupTableView()
        // 设置导航栏按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        // 注册原型 cell
        tableView?.register(UINib(nibName: "StatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellId)
        tableView?.register(UINib(nibName: "StatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
//        tableView?.register(demoCell.self, forCellReuseIdentifier: cellId)
        
        // 设置空cell提示文本
        tipString = "您还没有投资项目"
        // 设置预估行高
        tableView?.estimatedRowHeight = 300
        
        // 取消分割线
        tableView?.separatorStyle = .none
        
        setupNavTitle()
        
    }
    
    /// 设置导航标题
    private func setupNavTitle() {
        let button = TitleButton(title: NetworkTools.sharedTools.userAccount.screen_name)
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        navItem.titleView = button
    }
    @objc private func clickTitleButton(btn:UIButton) {
        btn.isSelected = !btn.isSelected
    }
}
